<?php
/**
 * 字符串处理类
 * 
 * @version $Id: String.php 187 2011-05-23 07:57:55Z linsir123 $
 */
class lpString
{
	/**
	 * 去除字符串的空格
	 */
	static function trim($string)
	{
		return preg_replace("/\s/", '', trim($string));
	}
	
	/**
	 * 使用反斜线引用字符串
	 */
	static function addslashes($var, $force = false)
	{
		if (!get_magic_quotes_gpc() || $force) 
			$var = is_array($var) ?
				array_map(array('lpString', 'addslashes'), $var) :
				addslashes($var);
		
		return $var;
	}
	
	/**
	 * 反转义使用反斜线引用的字符串
	 */
	static function stripslashes($var)
	{
		$var = is_array($var) ?
			array_map(array('lpString', 'stripslashes'), $var) :
			stripslashes($var);
		
		return $var;
	}
	
	/**
	 * SQL引号
	 */
	static function dbQuote($var)
	{
		$var = is_array($var) ?
			array_map(array('lpString', 'dbQuote'), $var) :
			'`'.trim($var).'`';
		
		return $var;
	}
	
	/**
	 * 转义SQL变量
	 */
	static function sqlEscape($var)
	{
		if (is_bool($var)) 
			$var = $var ? 1 : 0;
		elseif (is_null($var)) 
			$var = 'NULL';
		elseif (is_string($var))
			$var = "'".mysql_escape_string($var)."'";
		elseif (is_array($var))
			$var = array_map(array('lpString', 'sqlEscape'), $var);
			
		return $var;
	}
	
	/**
	 * 截取字符串，单位为字节
	 * 
	 * 以一个汉字为又字节，并不区分为UTF-8下的三字节
	 */
	static function substr($content, $length = 50, $omission = ' ...', $code = 'UTF-8')
	{
		$content = preg_replace("/\s/", ' ', strip_tags($content));
		$content = htmlspecialchars_decode($content);
		$content = str_replace(array("&nbsp;"), array(""), $content);
		if (empty($length) || strlen($content) <= $length) 
			return $content;
		
		$bytelen = 0;
		$wordstring = '';
		if (strtoupper($code) == 'UTF-8') {
			preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $content, $words);
			$count = count($words[0]);
			for ($i = 0; $i < $count; $i++) {
				$wordstring .= $words[0][$i];
				if (ord($words[0][$i]) >= 128) 
					$bytelen += 2;
				else
					$bytelen++;
				
				if ($bytelen >= $length) 
					return $wordstring . $omission;
			}
		} else {
			$count = strlen($content);
			for ($i = 0; $i < $count; $i++) {
				if(ord(substr($content, $i, 1)) >= 128) {
					$bytelen += 2;
					$wordstring .= substr($content, $i, 2);
					$i++;
				} else {
					$bytelen++;
					$wordstring .= substr($content, $i, 1);
				}
				
				if ($bytelen >= $length) 
					return $wordstring . $omission;
			}
		}
		
		return $content;
	}
	
	/**
	 * 将16进制颜色值转成RGB格式
	 */
	static function hex2rgb($color, $defualt = 'ffffff')
    {
		$color = strtolower($color);
		if (substr($color, 0, 2) == '0x')
		    $color = substr($color, 2);
		elseif (substr($color, 0, 1) == '#')
		    $color = substr($color, 1);
		    
		$l = strlen($color);
		if ($l == 3) {
		    $r = hexdec(substr($color, 0, 1));
		    $g = hexdec(substr($color, 1, 1));
		    $b = hexdec(substr($color, 2, 1));
		    
		    return array($r, $g, $b);
		} elseif ($l != 6) 
		    $color = $defualt;
		
		$r = hexdec(substr($color, 0, 2));
		$g = hexdec(substr($color, 2, 2));
		$b = hexdec(substr($color, 4, 2));
		
		return array($r, $g, $b);
    }
}
